|
|
|
| Recheck the Global Variable 1. Run this project and increment the Public variable several times. 2. Click the Override button. Youll see that the variable gintCount is never incremented. 3. Go back and click the Global command button. The value will start incrementing from where you left off last time. |  | Of course, if you preface your Public variables with the letter g and your module-level variables with an m and you use no prefix for local variables, youll never have this type of masking going on because you wont have a local variable name that starts with the letter g. |
|
|
|
|
|
|
Theres another type of declaration you can use for variables. You can declare variables with the keyword Static to indicate to Visual Basic that you want to preserve the contents of the variable between function calls. Variables declared with the Dim statement lose their value when a procedure ends and are reinitialized the next time the function is called. When you declare a variable with the keyword Static, the variable is only initialized once and does not lose its value between procedure calls. Variables declared with Static are allocated on the heap, an area of memory thats allocated when the program starts and is discarded when the program ends. |
|
|
|
|
| Static Variables Are Local 1. Run the sample project. 2. Click the Variables button. 3. Click the Static command button several times to view the incrementing of the number in the label. 4. Open the VarExamples.frm form in design mode and double-click the Static command button to view the following code: | | Private Sub cmdStatic_Click ()
Static intTimesClicked As Integer |
|
|
|